package com.ericsson.rm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.ericsson.rm.form.FileUploadCommand;
import com.ericsson.rm.service.FileUploadService;
import com.ericsson.rm.validator.FileUploadValidator;
import com.ericsson.rm.vo.ProjectDetailsVO;

@Controller
public class CSVFileUploadController {
	
	 private final static Logger logger = LoggerFactory.getLogger(CSVFileUploadController.class);
	  private final static String CLASS_NAME = CSVFileUploadController.class.getSimpleName();
	  private final static String FILE_UPLOAD_FORM = "csvFileUploadd";
	  HashMap<String, Object> map = new HashMap<String, Object>();
	  @Autowired
	  FileUploadService fileUploadService;
	  
	 
	  public FileUploadService getFileUploadService() {
		return fileUploadService;
	}

	public void setFileUploadService(FileUploadService fileUploadService) {
		this.fileUploadService = fileUploadService;
	}

	public FileUploadValidator getValidator() {
		return validator;
	}

	public void setValidator(FileUploadValidator validator) {
		this.validator = validator;
	}
	FileUploadValidator validator ;
	  
	  @Autowired
		public CSVFileUploadController(FileUploadValidator  fileValidator){
			if(logger.isDebugEnabled()){
			      logger.debug(CLASS_NAME+" constructor entering...");
			  }
			this.validator = fileValidator;   
		}
		  
		  @RequestMapping("FileUpload")
		  public String initForm(ModelMap model)
		  {
		    final String METHOD_NAME = CLASS_NAME + ".initForm() ";
		    System.out.println("Init method......................................");
		    if(logger.isDebugEnabled()){
		      logger.debug(METHOD_NAME+"entering...");
		    } 
		 
		    FileUploadCommand fileuploadCommand = new FileUploadCommand();
		    model.addAttribute("FileUploadCommand",fileuploadCommand);
		    return FILE_UPLOAD_FORM;
		  }
		  @RequestMapping("saveFile")
		  public ModelAndView uploadfile(HttpServletRequest req ){
			  try{
				  
				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req ; 
				MultipartFile multipartFile = multipartRequest.getFile("upfile");
				String file = new String(multipartFile.getBytes()); 
				String[] split = file.split("\n");
				List<ProjectDetailsVO> project_list = new ArrayList<ProjectDetailsVO>();
				for(int i=0;i<split.length;i++){
					System.out.println(split[i]);
					String[] splitIn1=split[i].split(",");
					ProjectDetailsVO  projVo = new ProjectDetailsVO();
					for(int j=0;j<splitIn1.length;j++){
					//	System.out.println(splitIn1[j]);
						projVo.setProjectId(splitIn1[0]);
						projVo.setProjectName(splitIn1[1]);
						projVo.setProject_start_date(new Date());
						projVo.setProject_end_date(new Date());
						projVo.setRegion(splitIn1[4]);
						projVo.setStatus(splitIn1[5]);
						projVo.setLocation(splitIn1[6]);
						projVo.setNo_of_resource(splitIn1[7]);
						projVo.setProject_manager_signum(splitIn1[8]);
					//	project_list.add(projVo);
						
					}
					
					project_list.add(projVo);
				}
				
				fileUploadService.uploadCSVData(project_list);
				map.put("message", "CSV file Uploaded successfully");
			  }
			  catch(Exception e){
				  e.printStackTrace();
				  
			  }
			 
			  return new ModelAndView("csvFileUploadd","list",map) ;
		  }
		  

}
